﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kooboo.CMS.Sites.View;
using Kooboo.CMS.Sites.Models;
using Kooboo;
using Kooboo.CMS.Content.Models;
using System.Web;
using Kooboo.CMS.Content.Query;

namespace KB
{
    public class KoobooUtility
    {
        public static string GetPageUrl(string pageFullName, bool absolute = false)
        {
            var url = Page_Context.Current.FrontUrl.PageUrl(pageFullName).ToString();
            return absolute ? ToAbsoluteUrl(url) : url;
        }
        public static string ToAbsoluteUrl(string url)
        {
            if (url.StartsWith("/"))
            {
                return HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + url;
            }
            return url;
        }

        public static string GetAllQueryString(string key, string @default)
        {
            return Page_Context.Current.PageRequestContext.AllQueryString[key].AsString(@default);
        }
        public static string GetHtmlBlockBody(string name)
        {
            var block = new Kooboo.CMS.Sites.Services.HtmlBlockManager();
            return block.Get(Site.Current, name).Body;
        }
        public static string GetViewBody(string name)
        {
            var view = new Kooboo.CMS.Sites.Services.ViewManager();
            return view.Get(Site.Current, name).Body;
        }
        public static object ViewData(string key, object value = null)
        {
            if (value != null)
            {
                Page_Context.Current.ControllerContext.Controller.ViewData[key] = value;
            }
            return Page_Context.Current.ControllerContext.Controller.ViewData[key];
        }
        public static object GetViewParameterValue(string paramName)
        {
            var parameter = Page_Context.Current[paramName];
            return parameter;
        }
        public static IEnumerable<object> GetViewParameterValues(string paramName, string viewName = "")
        {
            foreach (var position in MenuHelper.Current().PagePositions)
            {
                if (position is ViewPosition)
                {
                    var view = (ViewPosition)position;
                    if ((viewName == "" || view.ViewName.EqualsIgnoreCase(viewName)) && view.Parameters != null)
                    {
                        foreach (var param in view.Parameters)
                        {
                            if (param.Name.EqualsIgnoreCase(paramName))
                            {
                                yield return param.Value;
                                break;
                            }
                        }
                    }
                }
            }
        }

        public static string GetCustomField(string key)
        {
            return Site.Current.CustomFields.GetString(key);
        }

        public static string GenerateUUID(int numDigits = 16)
        {
            return UniqueIdGenerator.GetInstance().GetBase32UniqueId(numDigits);
        }
        public static bool IsVisitor
        {
            get
            {
                return Page_Context.Current.PageRequestContext == null || Page_Context.Current.PageRequestContext.RequestChannel == Kooboo.CMS.Sites.Web.FrontRequestChannel.Host ||
              Page_Context.Current.PageRequestContext.RequestChannel == Kooboo.CMS.Sites.Web.FrontRequestChannel.HostNPath;
            }
        }
        public static MediaContent SaveFile(HttpPostedFileBase file, string folder, string fileName = "", Repository repository = null)
        {
            if (repository == null)
            {
                repository = Repository.Current;
            }
            if (file == null || file.ContentLength == 0)
            {
                return null;
            }
            var contentFolder = ContentHelper.MediaFolder(folder);
            var manange = new Kooboo.CMS.Content.Services.MediaContentManager();
            if (fileName.IsNullOrEmpty())
            {
                fileName = file.FileName;
                var i = 0;
                while (i < 1000 && contentFolder.CreateQuery().WhereEquals("FileName", fileName).Count() > 0)
                {
                    fileName = file.FileName.Insert(file.FileName.LastIndexOf('.'), "_" + (++i).ToString());
                }
            }
            var content = manange.Add(repository, contentFolder, fileName, file.InputStream, true);
            return content;
        }

    }
}